<?php
namespace  app\manage\model;
use think\Db;
use think\Model;
class Action extends Model{
    protected  $table = 'action';

    /**
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function m_list() {
        $data = Db::name($this->table)
            ->select();

        $data = $this->deal_list_to_tree($data);
        return $data;
    }

    /**
     * 未经树状结构处理的权限
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function m_list_y() {
        $data = Db::name($this->table)
            ->select();

        return $data;
    }

    /**
     * 处理数据
     * @param $data
     * @param string $pkName
     * @param string $pIdName
     * @param string $childName
     * @param bool $is_empty_childrens
     * @param string $rootId
     * @return array
     */
    private function deal_list_to_tree($data, $pkName='id', $pIdName='pid', $childName='children_list', $is_empty_childrens=false, $rootId=''){
        $new_data = [];
        foreach($data as $sorData){
            if($sorData[$pIdName] == $rootId){
                $res = $this->deal_list_to_tree($data, $pkName, $pIdName, $childName, $is_empty_childrens, $sorData[$pkName]);
                if(!empty($res) && !$is_empty_childrens){
                    if(array_key_exists($childName, $sorData)) {
                        if(array_key_exists($childName, $sorData)){
                            $sorData[$childName][] = $res[0];
                        }else{
                            $sorData[$childName][] = $res;
                        }
                    }else{
                        $sorData[$childName] = $res;
                    }
                }
                $new_data[] = $sorData;
            }
        }
        return $new_data;
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($action_id, $data) {
        $data = Db::name($this->table)
            ->where(['id'=>$action_id])
            ->update($data);
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_delete($action_id) {
        $data = Db::name($this->table)
            ->where(['id'=>$action_id])
            ->delete();
        return $data;
    }

    /**
     * @param $action_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($action_id) {
        $data = Db::name($this->table)
            ->select();
        $data = $this->deal_list_to_tree($data);

        foreach ($data as $ka => $a) {
            if (!isset($a['children_list']) || !is_array($a['children_list'])) {
                continue;
            }
            foreach ($a['children_list'] as $kb => $b) {
                if (!isset($b['children_list']) || !is_array($b['children_list'])) {
                    continue;
                }
                foreach ($b['children_list'] as $kc => $c) {
                    if (in_array($data[$ka]['children_list'][$kb]['children_list'][$kc]['id'], $action_id)) {
                        $data[$ka]['children_list'][$kb]['children_list'][$kc]['status'] = true;
                    } else {
                        $data[$ka]['children_list'][$kb]['children_list'][$kc]['status'] = false;
                    }
                }
            }
        }
        return $data;

        //        $data = Db::name($this->table)
//            ->where(['id'=>$action_id])
//            ->field('id,action,url,pid')
//            ->find();
//        if ($data['pid'] !== 0) {
//            $actions[] = $data['action'];
//
//        }
//    }
    }

    /**
     * 超管权限
     * @return array
     */
    public function m_list_admin() {
        return ['13','110','10','11','12','100','101','102','103','104','105','106','107','108','109'];
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detailOne($id) {
        $data = Db::name($this->table)
            ->where('id', $id)
            ->find();
        return $data;
    }

}